<template>
 <div>
    <el-row :gutter="10">
       <NavbarVote/>
    </el-row>
     <div v-if="vote.voteMode==2">
        <voteOnline :vote="vote" :backList="backList"/>
     </div>

    <div v-else-if="vote.voteMode==1">
      <VoteOnlineCurr :vote="vote" :backList="backList"/>
    </div>

    <div v-else class="app-container">
      <!-- 搜索工作栏 -->
      <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="投票名称" prop="voteName">
          <el-input v-model="queryParams.voteName" placeholder="请输入投票名称" clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form> -->

      <!-- 列表 -->
      <el-table v-loading="loading" :data="list">
        <el-table-column label="投票名称" align="center" prop="voteName" />
        <el-table-column label="投票开始时间" align="center" prop="beginTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.beginTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="投票结束时间" align="center" prop="endTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.endTime<new Date()">已结束</el-tag>
            <el-tag type="success" v-else-if="scope.row.beginTime>new Date()">未开始</el-tag>
            <el-tag type="danger" v-else-if="scope.row.beginTime<new Date() && scope.row.endTime>new Date()">进行中</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="被投票人数" align="center" prop="importCount" /> -->
        <el-table-column label="是否已投票" align="center" prop="isVote">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.isVote">已投票</el-tag>
            <el-tag type="success" v-else>未投票</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-if="!scope.row.isVote && scope.row.beginTime<new Date() && scope.row.endTime>new Date()" slot-scope="scope">
            <!-- <template slot-scope="scope"> -->
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAdd(scope.row)"
                      v-hasPermi="['vote:online:query']">开始投票</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/> -->

      <!-- 对话框(添加 / 修改) -->
      <!-- <el-dialog :title="title" :visible.sync="open" width="1000" append-to-body>

      </el-dialog> -->
      </div>



    <el-dialog :title="selectRuleTitle" :visible.sync="showRule" width="400px" append-to-body>
        <el-select filterable v-model="voteId" placeholder="请选择">
          <el-option
              v-for="item in voteList"
              :key="item.id"
              :label="item.voteName2"
              :value="item.id"
          ></el-option>
        </el-select>
      <div slot="footer" class="dialog-footer o-footer">
        <el-button type="primary" @click="submitVote">确认</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { NavbarVote} from '@/layout/components'
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import VoteOnline from "./onlinevote.vue";
import VoteOnlineCurr from "./onlinevotecurr.vue";
import {getByUser} from "@/api/vote/info";
export default {
  name: "online",
  components: {
    VoteOnline,
    VoteOnlineCurr,
    NavbarVote
  },
  data() {
    return {
      // 遮罩层
      loading: true,

      list: [],

      toatl: 0,

      voteId: null,
      // 投票信息
      vote: {},
      // 投票模式
      voteMode: null,
      // 投票列表
      voteList: [],

      // 是否显示头片规则
      showRule: false,
      // 弹出层标题
      selectRuleTitle: '请选择投票规则',

      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        voteName: null,
        voteCount: null,
        limitCount: null,
        remark: null,
        voteType: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      voteTypeDictDatas: getDictDatas(DICT_TYPE.VOTE_RULE_TYPE),
    };
  },
  created() {
    // this.getByUser();
    this.getList();
  },
  methods: {
    /**
     * 获取当前用户的投票信息
     */
     getByUser(){
      getByUser({voteType:1}).then(resp=>{
        if(resp.data.length<=0){
          this.$modal.msgSuccess("您当前未有待评审的投票规则");
          // this.votedPrompt = '您当前未有待评审的投票规则';
          // this.userIsVoted = true;
          return;
        }
        if(resp.data.length==1){
          this.vote = resp.data[0];
        }else{
          this.showRule=true;
          this.voteList = resp.data;
          for(let vote of this.voteList){
            if(vote.isVote){
              vote.voteName2=vote.voteName+'-已投票';
            }else{
              vote.voteName2=vote.voteName;
            }
          }
        }
       
      })
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      
      getByUser({voteType:1}).then(resp=>{
        console.log(resp)
        if(resp.data.length<=0){
          this.loading=false;
          this.$modal.msgSuccess("您当前未有待评审的投票规则");
          // this.votedPrompt = '您当前未有待评审的投票规则';
          return;
        }
        this.list = resp.data;
        this.total = resp.data.length;
        this.loading = false;
      })
      
    },


    backList(){
      this.vote = {};
      this.getList();
    },

    handleAdd(row){
      this.vote=row;
    },

    /**
     * 选择投票规则提交
     */
    submitVote(){
      if(this.voteId==null){
        this.$modal.msgError("请选择投票规则");
        return;
      }
      this.showRule=false;
      for(let vote of this.voteList){
        if(vote.id==this.voteId){
          this.vote=vote;
          return;
        }
      }
    },



    /** 取消按钮 */
    closeVoteOnline() {
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        voteName: undefined,
        beginTime: undefined,
        endTime: undefined,
        voteCount: undefined,
        limitCount: undefined,
        remark: undefined,
        importCount: undefined,
      };
      this.disabledVoteType =false;
      this.resetForm("form");
    },
    
   
  }
};
</script>
